package org.nch.videoLauncher.repository;

import org.nch.videoLauncher.domain.VideoItem;
import org.hibernate.Session;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Nov 14, 2008
 * Time: 2:34:51 PM
 * To change this template use File | Settings | File Templates.
 */
public class VideoItemRepository {



    public VideoItemRepository(){
    }
    
    public VideoItem getById(Integer id){

        VideoItem videoItem;
        Session session = HibernateUtil.getSession();

        try {
            videoItem = (VideoItem) session.load(VideoItem.class, id);
        }
        finally {
            session.close();
        }

        return videoItem;
    }

    public List<VideoItem> getAll(){

        List <VideoItem> videoItems;
        Session session = HibernateUtil.getSession();

        try {
            videoItems = session.createQuery("from VideoItem as vi order by vi.title asc").list();
        }
        finally {
            session.close();
        }

         return videoItems;
    }

    public List<VideoItem> getAllMovies(){

        List <VideoItem> videoItems;
        Session session = HibernateUtil.getSession();

        try {
            videoItems = session.createQuery("from VideoItem as vi where (vi.sportsEvent.sport = ? and vi.tvInfo.show = ?) OR (vi.tvInfo = null and vi.sportsEvent = null) order by vi.title asc").setString(0, "").setString(1, "").list();
        }
        finally {
            session.close();
        }

         return videoItems;
    }

    public List<VideoItem> getAllTvShows(){

        List <VideoItem> videoItems;
        Session session = HibernateUtil.getSession();

        try {
            videoItems = session.createQuery("from VideoItem as vi where (vi.tvInfo.show <> ? and vi.tvInfo.show <> null) order by vi.title asc").setString(0, "").list();
        }
        finally {
            session.close();
        }

         return videoItems;
    }

    public List<VideoItem> getAllSportsEvents(){

        List <VideoItem> videoItems;
        Session session = HibernateUtil.getSession();

        try {
            videoItems = session.createQuery("from VideoItem as vi where (vi.sportsEvent.sport <> ? and vi.sportsEvent.sport <> null) order by vi.title asc").setString(0, "").list();
        }
        finally {
            session.close();
        }

         return videoItems;
    }

    public List<VideoItem> getQuickSearchResults(String searchText){

        List <VideoItem> videoItems;
        Session session = HibernateUtil.getSession();
        String query = "%" + searchText + "%";

        try {
            videoItems = session.createQuery("from VideoItem as vi where (lower(vi.title) like lower(?) or lower(vi.description) like lower(?) or lower(vi.keywords) like lower(?)) order by vi.title asc").setString(0, query).setString(1, query).setString(2, query).list();
        }
        finally {
            session.close();
        }

         return videoItems;
    }

    public List<VideoItem> getMovieSearchResults(HashMap searchTerms){

        List <VideoItem> videoItems;
        Session session = HibernateUtil.getSession();
        Criteria criteria = session.createCriteria(VideoItem.class);

        criteria.add(Restrictions.isNotNull("imdbInfo.title"));
        criteria.addOrder( Order.asc("title"));

        if (searchTerms.containsKey("director"))
            criteria.add(Restrictions.ilike("imdbInfo.directedBy", (String) searchTerms.get("director"), MatchMode.ANYWHERE));
        if (searchTerms.containsKey("cast"))
            criteria.add(Restrictions.ilike("imdbInfo.cast", (String) searchTerms.get("cast"), MatchMode.ANYWHERE));        
        if (searchTerms.containsKey("genre"))
            criteria.add(Restrictions.ilike("imdbInfo.genre", (String) searchTerms.get("genre"), MatchMode.ANYWHERE));

        try {
            videoItems = criteria.list();
        }
        finally {
            session.close();
        }

         return videoItems;
    }

    public List<VideoItem> getTvSearchResults(HashMap searchTerms){

        List <VideoItem> videoItems;
        Session session = HibernateUtil.getSession();
        Criteria criteria = session.createCriteria(VideoItem.class);

        criteria.add(Restrictions.isNotNull("tvInfo.show"));
        criteria.addOrder( Order.asc("title"));

        if (searchTerms.containsKey("show"))
            criteria.add(Restrictions.ilike("tvInfo.show", (String) searchTerms.get("show"), MatchMode.ANYWHERE));
        if (searchTerms.containsKey("season"))
            criteria.add(Restrictions.eq("tvInfo.season", (Integer) searchTerms.get("season")));
        if (searchTerms.containsKey("episode"))
            criteria.add(Restrictions.eq("tvInfo.episode", (Integer) searchTerms.get("episode")));

        try {
            videoItems = criteria.list();
        }
        finally {
            session.close();
        }

         return videoItems;
    }

    public List<VideoItem> getSportsSearchResults(HashMap searchTerms){

        List <VideoItem> videoItems;
        Session session = HibernateUtil.getSession();
        Criteria criteria = session.createCriteria(VideoItem.class);

        criteria.add(Restrictions.isNotNull("sportsEvent.team"));
        criteria.addOrder( Order.asc("title"));

        if (searchTerms.containsKey("team"))
            criteria.add(Restrictions.ilike("sportsEvent.team", (String) searchTerms.get("team"), MatchMode.ANYWHERE));
        if (searchTerms.containsKey("season"))
            criteria.add(Restrictions.ilike("sportsEvent.season", (String) searchTerms.get("season"), MatchMode.ANYWHERE));
        if (searchTerms.containsKey("sport"))
            criteria.add(Restrictions.ilike("sportsEvent.sport", (String) searchTerms.get("sport"), MatchMode.ANYWHERE));

        try {
            videoItems = criteria.list();
        }
        finally {
            session.close();
        }

         return videoItems;
    }

    public VideoItem persist(VideoItem vi){

        Session session = HibernateUtil.getSession();

        try {
            session.beginTransaction();
            session.saveOrUpdate(vi);
            session.getTransaction().commit();
        }
        finally {
            session.close();
        }

        return vi;
    }

    public List<VideoItem> persistAll(List<VideoItem> videoItems){

        Session session = HibernateUtil.getSession();

        try {
            session.beginTransaction();
            for  (VideoItem vi : videoItems ){
                session.saveOrUpdate(vi);
            }
            session.getTransaction().commit();
        }
        finally {
            session.close();
        }

        return videoItems;
    }

    public void delete(VideoItem vi){

        Session session = HibernateUtil.getSession();

        try {
            session.beginTransaction();
            session.delete(vi);
            session.getTransaction().commit();
        }
        finally {
            session.close();
        }
    }

}
